package com.whut.cristiano.autographbook.Activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.whut.cristiano.autographbook.R;
import com.whut.cristiano.autographbook.Util.MD5Util;
import com.whut.cristiano.autographbook.Util.NetUtil;
import com.whut.cristiano.autographbook.Util.SharedPrefUtil;

public class LoginActivity extends BaseActivity {

    private EditText accountText;
    private EditText passwordText;
    private Button loginBtn;//登录
    private Button registerBtn;//注册
    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        if(SharedPrefUtil.getLoginState()){
            Intent intent = new Intent(this,MainActivity.class);
            startActivity(intent);
            finish();
        }
        initView();

    }
    @Override
    public void initView(){
        this.getSupportActionBar().hide();
        accountText = (EditText) findViewById(R.id.account);
        passwordText = (EditText) findViewById(R.id.password);
        loginBtn = (Button)findViewById(R.id.login);
        registerBtn = (Button) findViewById(R.id.go_to_register);
        loginBtn.setOnClickListener(this);
        registerBtn.setOnClickListener(this);
        progressDialog = new ProgressDialog(this);
        TextInputLayout accountWrapper = (TextInputLayout) findViewById(R.id.accountWrapper);
        TextInputLayout passwordWrapper = (TextInputLayout) findViewById(R.id.passwordWrapper);
        accountWrapper.setHint("手机号");
        passwordWrapper.setHint("密码");
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login:
                String account = accountText.getText().toString();
                String password = passwordText.getText().toString();
                if(account==null||password==null||account.trim().equals("")||password.trim().equals("")){
                    break;
                }else{
					if(account.equals("test")&&password.equals("test")){
						SharedPrefUtil.saveUserName("测试者");
                            SharedPrefUtil.saveUserPhone("test");
                            SharedPrefUtil.saveUserSchool("武汉理工大学");
                            SharedPrefUtil.saveLoginState(true);
                            SharedPrefUtil.saveUserPhoto("http://autographbook.oss-cn-hangzhou.aliyuncs.com/33114461_p3.jpg");
					Intent intent2 = new Intent(LoginActivity.this,MainActivity.class);
					startActivity(intent2);
					finish();
					break;
					}else{
						showDialog(progressDialog);
						NetUtil.Login(account, MD5Util.encodeToHex(password),LoginActivity.this,progressDialog);
						break;
					}
                    /**showDialog(progressDialog);
                    NetUtil.Login(account, MD5Util.encodeToHex(password),LoginActivity.this,progressDialog);
                    break;*/
                }

            case R.id.go_to_register:
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
        }
    }

}
